<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="edu.columbia.ubms.ejb.UserInfoSessionBeanRemote" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Register</title>
<link rel="stylesheet" type="text/css" href="ubms.css" media="screen" />
<%!
private boolean empty(String str)
{
	if("".equalsIgnoreCase(str) || null == str)
		return true;
	else
		return false;
}

private boolean lenCheck(String str)
{
	if(empty(str) || str.length() > 16)
		return true;
	else
		return false;
}

public boolean isValid(StringBuffer msg, String firstName, String lastName, String eMail, String userName, String password) {
	
	 String fn = firstName;
	 String ln = lastName;
	 String em = eMail;
	 String id = userName;
	 String pw = password;

	 boolean valid = true;
	 if(lenCheck(id))
	 {
	 	msg.append("Username should be any alphanumeric string with 16 characters or less.\n");
	 	valid = false;
	 }
	 if(lenCheck(pw))
	 {
		 msg.append("Password should be any alphanumeric string with 16 characters or less.\n");
		 valid = false;
	 }
	 if (!empty(fn)) {
		 if(lenCheck(fn))
		 {
		 	msg.append("First Name should be any alphanumeric string with 16 characters or less.\n");
		 	valid = false;
		 }
	 }
	 if (!empty(ln)) {
		 if(lenCheck(ln))
		 {
		 	msg.append("Last Name should be any alphanumeric string with 16 characters or less.\n");
		 	valid = false;
		 }
	 }	 

	 Pattern pattern;
	 Matcher matcher;
		// user name special</head>

    if(!lenCheck(id))
    {
		 pattern = 
	            Pattern.compile("[^0-9a-zA-Z()-_]");

	     matcher = 
	            pattern.matcher(id);
	     if(matcher.find()) 
	     {
	    	 msg.append("Invalid user name.\n");
	    	 valid = false;
	     }
    }
	 
	// email validation
	if (!empty(em)) {
	    if(!lenCheck(em))
	    {
			 pattern = Pattern.compile("[0-9a-zA-Z()-_.]+@[0-9a-zA-Z()-_.]+");
		     matcher = pattern.matcher(em);
		     if(!matcher.find()) 
		     {
		    	 msg.append("Invalid Email.\n");
		    	 valid = false;
		     } else {				 
			     pattern = Pattern.compile("[^0-9a-zA-Z-._@]");
			     matcher = pattern.matcher(id);
			     if(matcher.find()) 
			     {
			    	 msg.append("Invalid Email.\n");
			    	 valid = false;
			     }
		     }
	    }
	}
    System.out.println("valid : " + valid);
    System.out.println("error : " + msg.toString());
    
    return valid;
}
%>

<%
	String firstName = request.getParameter("firstname");
	String lastName = request.getParameter("lastname");
	String email = request.getParameter("email");
	String userName = request.getParameter("username");
	String password = request.getParameter("password");
	
	StringBuffer msg = new StringBuffer();
	boolean isValid = isValid(msg, firstName, lastName, email, userName, password);
	
	String nextURL = null;
	if (isValid) {
		try {   
		    Context ctx = new InitialContext();		
			UserInfoSessionBeanRemote beanRemote = (UserInfoSessionBeanRemote)ctx.lookup("UserInfoSessionBean/remote");
			boolean userExists = beanRemote.userDupCheck(userName);
			
			if (userExists) {
				msg.append("username is duplicated");
				out.println("username is duplicated");
				nextURL = "userRegisterForm.jsp";
				//response.sendRedirect("userRegisterForm.jsp");
			} else {
				msg.append("Successfully registered.");
				int userId = beanRemote.userRegister(userName, firstName, lastName, password, email);
				
				session.setAttribute("userid", userId);
				nextURL = "mainPageFrame.jsp";
				//response.sendRedirect("mainPageFrame.jsp");
			}
		} catch(Exception e) {
		    out.println("Error:" + e.getMessage());
		}	
	} else {
		nextURL = "userRegisterForm.jsp";
	}
%>
<script language="javascript">
	alert("<%=msg.toString()%>");
	self.location.href="<%=nextURL%>";
</script>
</head>
<body>
</body>
</html>